package com.hzjy.api.controller;

import com.hzjy.api.service.BqTokenService;
import com.hzjy.api.service.impl.BqUserServiceImpl;
import com.hzjy.api.vo.*;
import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.redis.RedisCache;
import com.hzjy.common.utils.ServletUtils;
import com.hzjy.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 版权交易系统-登录
 *
 * @author SS
 */
@Api("用户登录")
@RestController
@RequestMapping("/api/login")
public class ApiLoginController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ApiLoginController.class);
    @Autowired
    private BqUserServiceImpl bqUserService;
    @Autowired
    private BqTokenService bqTokenService;
    @Autowired
    private RedisCache redisCache;

    @ApiOperation("微信小程序登录")
    @PostMapping("/loginByWeiXinMini")
    public AjaxResult loginByWeiXin(@RequestBody WxLogin wxLogin) {
        if (wxLogin == null) {
            return AjaxResult.error(-1, "错误的请求");
        }
        Map map = bqUserService.loginByWeiXin(wxLogin);
        return AjaxResult.success(200,"登录成功",map);
    }

    @ApiOperation("网页登录-账号密码")
    @ApiImplicitParam(name = "bqLoginBody", value = "网页登录", dataType = "BqLoginBody")
    @PostMapping("/loginByWebUP")
    public AjaxResult loginByWebUP(@Validated @RequestBody BqLoginBody bqLoginBody) {
        String token = bqUserService.loginByWebUP(bqLoginBody);
        log.info("【网页登录-账号密码】,token:" + token);
        return AjaxResult.success(200, "登录成功", token);
    }

    @ApiOperation("网页登录-账号短信")
    @ApiImplicitParam(name = "bqLoginBody", value = "网页登录", dataType = "BqLoginBody")
    @PostMapping("/loginByWebUD")
    public AjaxResult loginByWebUD(@Validated @RequestBody BqPhoneLoginBody bqPhoneLoginBody) {
        //校验短信验证码
        String code = redisCache.getCacheObject(bqPhoneLoginBody.getPhone());
        if (StringUtils.isNull(code)) {
            return AjaxResult.error("注册用户" + bqPhoneLoginBody.getPhone() + "失败，验证码已过期");
        }
        if (!StringUtils.equals(code, bqPhoneLoginBody.getCode())) {
            return AjaxResult.error("注册用户" + bqPhoneLoginBody.getPhone() + "失败，验证码错误");
        }
        String token = bqUserService.loginByWebUD(bqPhoneLoginBody);
        log.info("【网页登录-账号短信】,token:" + token);
        return AjaxResult.success(200, "登录成功", token);
    }


    @ApiOperation("网页注册")
    @ApiImplicitParam(name = "bqRegisterBody", value = "网页注册", dataType = "BqRegisterBody")
    @PostMapping("/registerByWeb")
    public AjaxResult registerByWeb(@Validated @RequestBody BqRegisterBody bqRegisterBody) {
        if (StringUtils.isNotNull(bqUserService.findByUserName(bqRegisterBody.getUsername()))) {
            return AjaxResult.error("注册用户" + bqRegisterBody.getUsername() + "失败，用户名已存在");
        } else if (StringUtils.isNotNull(bqUserService.findByPhone(bqRegisterBody.getPhone()))) {
            return AjaxResult.error("注册用户" + bqRegisterBody.getUsername() + "失败，手机号已存在");
        }
        //校验短信验证码
        String code = redisCache.getCacheObject(bqRegisterBody.getPhone());
        if (StringUtils.isNull(code)) {
            return AjaxResult.error("注册用户" + bqRegisterBody.getUsername() + "失败，验证码已过期");
        }
        if (!StringUtils.equals(code, bqRegisterBody.getCode())) {
            return AjaxResult.error("注册用户" + bqRegisterBody.getUsername() + "失败，验证码错误");
        }
        bqUserService.registerByWeb(bqRegisterBody);
        return AjaxResult.success("注册成功");
    }


    @PostMapping("/test")
    public void test() {
        System.out.println("成功进入test");
        BqLoginUser loginUser = bqTokenService.getBqLoginUser(ServletUtils.getRequest());
        System.out.println(loginUser.getBqUser().getUserId());
    }

    @PostMapping("/test2")
    public void test2() {
        System.out.println("成功进入test2");
    }

}
